package com.net.xpay.stat.scheduler;

import com.google.common.base.Strings;
import com.net.common.exception.BusinessException;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.UserShard;
import com.net.xpay.common.domain.partner.PartnerWithdrawRecordBatch;
import com.net.xpay.common.enums.config.CommonSysConfigKey;
import com.net.xpay.common.enums.partner.WithdrawStatus;
import com.net.xpay.common.helper.CommonSysConfigHelper;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.UserShardManager;
import com.net.xpay.common.manager.partner.PartnerWithdrawRecordBatchManager;
import com.net.xpay.common.model.search.PartnerWithdrawRecordBatchSearchModel;
import com.net.xpay.common.service.PartnerWithdrawService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.shard.ShardUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 2019/12/18 20:01

 * 批量提现结果查询
 */
@Slf4j
@Component
public class PartnerWithdrawRecordBatchQueryScheduler {

    @Autowired
    private PartnerWithdrawRecordBatchManager partnerWithdrawRecordBatchManager;

    @Autowired
    private PartnerWithdrawService partnerWithdrawService;

    @Autowired
    private CommonSysConfigHelper commonSysConfigHelper;

    @Autowired
    private UserManager userManager;

    @Autowired
    private UserShardManager userShardManager;


    /**
     * 提现转账批次查询
     */
    @Scheduled(cron = "*/10 * * * * *")
    public void queryBatch() {
        PartnerWithdrawRecordBatchSearchModel searchModel = new PartnerWithdrawRecordBatchSearchModel();
        searchModel.setStatus(WithdrawStatus.DEALING);
        searchModel.setAuto(true);
        List<PartnerWithdrawRecordBatch> batches = partnerWithdrawRecordBatchManager.search(searchModel);
        log.info("queryBatch batched={}", SerializationUtil.obj2String(batches));
        if (CollectionUtils.isEmpty(batches)) {
            return;
        }
        //打款支付宝和打款手机号
        String alipayAccount = commonSysConfigHelper.getString(CommonSysConfigKey.PARTNER_WITHDRAW_ALIPAY_ACCOUNT);
        String sysMobile = commonSysConfigHelper.getString(CommonSysConfigKey.PARTNER_WITHDRAW_MOBILE);
        if (Strings.isNullOrEmpty(alipayAccount) || Strings.isNullOrEmpty(sysMobile)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请配置系统打款账号");
        }

        User transUser = userManager.findByMobile(sysMobile);
        if (transUser == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "系统打款账号不存在");
        }
        UserShard shard = userShardManager.getById(transUser.getId());

        try {
            ShardUtil.setShardContext(shard.getShardId());
            for (PartnerWithdrawRecordBatch batch : batches) {
                try {
                    partnerWithdrawService.updatePartnerWithdrawBatchResult(batch, transUser);
                } catch (Exception e) {
                    log.warn("queryBatch error|batch={}", SerializationUtil.obj2String(batch), e);
                }
            }
        } finally {
            ShardUtil.cleanShardContext();
        }
    }
}
